package com.csgc.camera.biz.other

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import androidx.activity.viewModels
import com.csgc.camera.base.BaseBindingActivity
import com.csgc.camera.biz.other.activity.ChooseMaskStyleActivity
import com.csgc.camera.http.viewmodel.AppViewModel
import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ActivityCodeInputBinding
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.ToastUtils
import com.hlyj.http.base.tool.lib_hlyj_base.bean.BaseResult
import com.hlyj.http.base.tool.lib_hlyj_base.net.ApiCallback
import com.ok.common.api.GlobalApi

class CodeInputActivity :
    BaseBindingActivity<ActivityCodeInputBinding>(ActivityCodeInputBinding::inflate) {
    private var countdownTimer: android.os.CountDownTimer? = null
    private val viewModel: AppViewModel by viewModels()
    private val phone: String by lazy {
        intent.getStringExtra("phone") ?: ""
    }

    companion object {
        fun launch(context: Context, phone: String) {
            val intent = Intent(context, CodeInputActivity::class.java)
            intent.putExtra("phone", phone)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            fitsSystemWindows(true)
            //白色状态栏，黑色字体
            statusBarColor(R.color.white)
            statusBarDarkFont(true)
        }
        initlistener()
        initView()
    }


    private fun startCountdown() {
        countdownTimer = object : android.os.CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val seconds = millisUntilFinished / 1000
                binding.tvTimeCountDown.text = "${seconds}秒后"
                binding.tvResend.isEnabled = false
            }

            override fun onFinish() {
                binding.tvResend.isEnabled = true
            }
        }.start()
    }

    private fun initView() {
        startCountdown()
        binding.apply {
            tvSendInfo.text = "已发送验证码至${phone}"
            codeView.requestFocus()
        }
    }

    private fun initlistener() {

        binding.codeView.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Int
            ) {

            }

            override fun onTextChanged(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Int
            ) {
               p0?.let {
                   binding.btnLogin.isEnabled = p0.toString().length == 4
               }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })

        binding.btnLogin.clickWithTrigger {
            ChooseMaskStyleActivity.launch( this)
            finish()
        }

        binding.tvResend.clickWithTrigger {
            GlobalApi.getVerificationCode(phone, object : ApiCallback<BaseResult<Void?>?> {
                override fun onSuccess(result: BaseResult<Void?>?) {
                    ToastUtils.show("验证码已发送")
                    //binding.cvLoginCountdown.start()
                    startCountdown()
                }


                override fun onFailure(errorMessage: String) {
                    ToastUtils.show(errorMessage)
                }

                override fun onFinish() {

                }

            })
        }
        binding.btnClose.clickWithTrigger {
            finish()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        countdownTimer?.cancel()
    }
}